/**
 * @file      : main.c
 * @brief     : file brief
 * @author    : huenrong (huenrong1028@gmail.com)
 * @date      : 2021-04-09 16:22:29
 *
 * @copyright : Copyright (c) 2021 胡恩荣
 *
 */

#include <stdio.h>
#include <string.h>

#include "./user_list/user_list.h"

int main(int argc, char *argv[])
{
    int ret = -1;
    // 获取到的用户链表长度
    uint32_t user_list_len = -1;
    // 用户链表节点数据
    struct user_list_node_data node_data;

    // 初始化链表
    printf("-------------init user list-------------\n\n");
    user_list_init();

    // 获取链表长度
    printf("-------------get user list len-------------\n");
    user_list_len = get_user_list_len();
    printf("user_list_len = %d\n\n", user_list_len);

    // 查找节点"name_0"
    printf("-------------find node name_0-------------\n");
    ret = user_list_find("name_0");
    if (1 == ret)
    {
        printf("find node name_0\n\n");
    }
    else
    {
        printf("not found node name_0\n\n");
    }

    // 插入数据
    printf("-------------insert data to user list-------------\n");
    for (uint8_t i = 0; i < 5; i++)
    {
        if (0 == i)
        {
            snprintf(node_data.name, sizeof(node_data.name), "name_%d", 0);
            node_data.age = (0 * 10);
        }
        else
        {
            snprintf(node_data.name, sizeof(node_data.name), "name_%d", i);
            node_data.age = (i * 10);
        }

        int ret = user_list_add_tail(node_data);
        if (0 == ret)
        {
            printf("insert data, name:[%s], age: %d\n",
                   node_data.name, node_data.age);
        }
        else
        {
            printf("user_list_add_tail fail\n");

            return -1;
        }
    }

    // 获取链表长度
    printf("\n-------------get user list len-------------\n");
    user_list_len = get_user_list_len();
    printf("user_list_len = %d\n\n", user_list_len);

    // 遍历链表
    printf("-------------show all node data-------------\n");
    user_list_for_each_entry_safe_function();

    // 查找节点"name_0"
    printf("\n-------------find node name_0-------------\n");
    ret = user_list_find("name_0");
    if (1 == ret)
    {
        printf("find node name_0\n\n");
    }
    else
    {
        printf("not found node name_0\n\n");
    }

    // 获取节点"name_0"信息
    printf("-------------get node name_0 info-------------\n");
    memset(&node_data, 0, sizeof(struct user_list_node_data));
    ret = user_list_get_one_info(&node_data, "name_0");
    if (0 == ret)
    {
        printf("node name_0 info, name: [%s], age: %d\n\n",
               node_data.name, node_data.age);
    }
    else
    {
        printf("get node name_0 info fail\n\n");
    }

    // 更新节点"name_0"年龄为100
    printf("-------------update node name_0 age to 100-------------\n\n");
    memcpy(node_data.name, "name_0", strlen("name_0"));
    node_data.age = 100;
    ret = user_list_update(node_data);
    if (0 != ret)
    {
        printf("update node name_0 age fail\n");
    }

    // 获取节点"name_0"信息
    printf("-------------get node name_0 info-------------\n");
    memset(&node_data, 0, sizeof(struct user_list_node_data));
    ret = user_list_get_one_info(&node_data, "name_0");
    if (0 == ret)
    {
        printf("node name_0 info, name: [%s], age: %d\n\n",
               node_data.name, node_data.age);
    }
    else
    {
        printf("get node name_0 info fail\n\n");
    }

    // 删除节点"name_0"
    printf("-------------delete node name_0-------------\n");
    user_list_del(node_data);

    // 遍历链表
    printf("\n-------------show all node data-------------\n");
    user_list_for_each_entry_safe_function();

    // 获取链表长度
    printf("\n-------------get user list len-------------\n");
    user_list_len = get_user_list_len();
    printf("user_list_len = %d\n\n", user_list_len);

    // 删除所有节点
    printf("\n-------------delete all node-------------\n\n");
    user_list_del_all();

    // 获取链表长度
    printf("-------------get user list len-------------\n");
    user_list_len = get_user_list_len();
    printf("user_list_len = %d\n\n", user_list_len);

    return 0;
}
